package com.netdisk.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MyBatis-Plus 自动填充处理器
 */
@Component
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createdAt", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());
        // 添加对deletedAt字段的自动填充支持 - 对于需要自动设置删除时间的实体
        if (metaObject.hasGetter("deletedAt") && metaObject.hasSetter("deletedAt")) {
            // 只在字段为null时填充
            Object deletedAt = this.getFieldValByName("deletedAt", metaObject);
            if (deletedAt == null) {
                // 检查字段类型是否为LocalDateTime
                if (metaObject.getGetterType("deletedAt") == LocalDateTime.class) {
                    this.strictInsertFill(metaObject, "deletedAt", LocalDateTime.class, LocalDateTime.now());
                }
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updatedAt", LocalDateTime.class, LocalDateTime.now());
    }
} 